package com.cskaoyan.service;/**
 * @author mj
 * @date 2022-12-29 14:03
 * @describe
 */

import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.marketBean.*;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.mapper.marketMapper.MarketAdminMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author MJ
 * @date 2022-12-29 14:03
 * @describe
 */
@Service
public class MarketAdminMjServiceImpl implements MarketAdmin_MJ_Service {
    @Autowired
    MarketAdminMapper marketAdminMapper;
    @Override
    public CommonData<MJMarketAdminListBean> list(BasePageInfo info,String username) {
        List<MJMarketAdminListBean> mjMarketAdminListVOS = marketAdminMapper.selectByName(username);
        PageInfo<MJMarketAdminListBean> mjMarketAdminListBeanPageInfo = new PageInfo<>(mjMarketAdminListVOS);
        CommonData data = CommonData.data(mjMarketAdminListBeanPageInfo);
        // MarketAdminExample marketAdminExample = new MarketAdminExample();
        // MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        // criteria.andUsernameLike("%"+username+"%");
        // List<MJMarketAdminListVO> data = marketAdminMapper.selectByName(marketAdminExample);
        return data;
    }

    @Override
    public CommonData<MJMarketAdminOption> option() {
       List<MJMarketAdminOption> optionsList = marketAdminMapper.selectOption();
        PageInfo<MJMarketAdminOption> mjMarketAdminOptionPageInfo = new PageInfo<>(optionsList);
        CommonData data = CommonData.data(mjMarketAdminOptionPageInfo);
        return data;
    }

    @Override
    public MJMarketAdminCreateVO adminCreate(MJMarketAdminCreateVO param) {
        MJMarketAdminCreateVO mjMarketAdminCreateVO = new MJMarketAdminCreateVO();
        marketAdminMapper.insertAdmin(param);
        MarketAdmin mj = marketAdminMapper.selectByPrimaryKey(param.getId());

        mjMarketAdminCreateVO.setId(mj.getId());
        mjMarketAdminCreateVO.setPassword(mj.getPassword());
        mjMarketAdminCreateVO.setAddTime(new Date());
        mjMarketAdminCreateVO.setUpdateTime(new Date());
        mjMarketAdminCreateVO.setRoleIds(mj.getRoleIds());
        mjMarketAdminCreateVO.setUsername(mj.getUsername());

        return mjMarketAdminCreateVO;
    }

    @Override
    public MarketAdmin adminUpdate(MarketAdmin param) {
        marketAdminMapper.updateByPrimaryKey(param);
        return param;
    }

    @Override
    public MarketRole roleUpdate(MarketRole param) {
        marketAdminMapper.updateRole(param);
        return param;
    }

    @Override
    public void adminDelete(MarketAdmin param) {
        marketAdminMapper.deleteByPrimaryKey(param.getId());

    }

    @Override
    public void roleDelete(MarketRole param) {
        Integer id = param.getId();
        marketAdminMapper.deleteRoleById(id);
    }



    @Override
    public CommonData<MarketLog> adminLog(BasePageInfo info, String name) {
        List<MarketLog> marketLogs = marketAdminMapper.selectAdminLog(name);
        PageInfo<MarketLog> marketLogPageInfo = new PageInfo<>(marketLogs);
        CommonData data = CommonData.data(marketLogPageInfo);
        return data;
    }

    @Override
    public CommonData<MarketRole> roleList(BaseRespVo info, String name) {
        List<MarketRole> marketRoleList =marketAdminMapper.selectRole(name);
        PageInfo<MarketRole> marketRolePageInfo = new PageInfo<>(marketRoleList);
        CommonData data = CommonData.data(marketRolePageInfo);
        return data;
    }

    @Override
    public MarketRole roleInsert(MarketRole param) {
        marketAdminMapper.insertRole(param);
        Integer id = param.getId();
        MarketRole marketRole = marketAdminMapper.selectRoleById(id);
        marketRole.setAddTime(new Date());
        marketRole.setUpdateTime(new Date());
        return marketRole;
    }


}
